package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import com.example.utils.JwtHelper;
import com.example.utils.Result;
import com.example.utils.ResultCodeEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Yun
 * @Date: 2024/3/14
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;


    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        Result result = userService.login(user);
        return result;
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader String token) {
        Result result = userService.getUserInfo(token);
        return result;
    }


    @PostMapping("/checkUserName")
    public Result checkUserName(@Param("username") String username) {
        Result result = userService.checkUserName(username);
        return result;
    }


    @PostMapping("/regist")
    public Result register(@RequestBody User user) {
        Result result = userService.register(user);
        return result;
    }


    @GetMapping("/checkLogin")
    public Result checkLogin(@RequestHeader String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) return Result.build(null, ResultCodeEnum.NOTLOGIN);
        return Result.ok(null);
    }
}
